package com.seele.tigerwallet.ui.base;


import android.graphics.Typeface;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.seele.tigerwallet.R;
import com.seele.tigerwallet.ui.utils.StatusBarUtil;
import com.seele.tigerwallet.ui.utils.StatusBarUtils;
import com.seele.tigerwallet.ui.utils.SystemUtils;

public abstract class TitleBaseActivity extends BaseActivity {

    private LinearLayout mContainerLl;

    private static final int ETH_ID = 2361;
    public ImageView mBackImg;
    private TextView mTitleTv;
    public ImageView mWebViewBack;
    public RelativeLayout mTitleRv;
    public LinearLayout mTitleWebBackLl;
    private LinearLayout mBackLl;
    public ImageView mRightImg;
    public LinearLayout mBaseLl;
    public TextView mRightTv;

    private View mView;
    public View mSplitView;


    @Override
    protected int getLayoutId() {
        return R.layout.activtiy_title_base;
    }


    @Override
    protected void init() {

        initWidow();

        mContainerLl = findViewById(R.id.base_container);
        mSplitView = findViewById(R.id.split_view);
        mTitleRv = findViewById(R.id.title_rv);
        mBaseLl = findViewById(R.id.base_ll);
        mBackImg = findViewById(R.id.title_back_img);
        mTitleTv = findViewById(R.id.title_tv);
        mRightImg = findViewById(R.id.right_img);
        mRightTv = findViewById(R.id.right_tv);
        mWebViewBack = findViewById(R.id.title_web_back_img);
        mBackLl = findViewById(R.id.title_back_ll);

        mTitleWebBackLl = findViewById(R.id.title_web_back_ll);
//        Typeface font = Typeface.createFromAsset(this.getAssets(), "fonts/PING_FANG_SC_HEAVY.TTF");
//        mTitleTv.setTypeface(font);


        int layoutId = getContainerViewId();
        mView = View.inflate(this, layoutId, null);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        mView.setLayoutParams(layoutParams);
        addContainerView();
        initView();

        mBackImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mBackLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mRightImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onRightClick();
            }
        });
    }


    public void onRightClick() {
    }

    public abstract int getContainerViewId();


    protected abstract void initView();


    /**
     * 一个公共的Byid的接口
     *
     * @param id
     * @param <E>
     * @return
     */
    public final <E extends View> E $f(int id) {
        try {
            return (E) mView.findViewById(id);
        } catch (Exception ex) {
            Log.e("BaseFragment", "Could not cast View to concrete class.", ex);
            throw ex;
        }
    }


    public void addContainerView(View view) {
        if (null != view) {
            mContainerLl.addView(view);
        }

    }

    public void addContainerView() {
        if (null != mView) {
            mContainerLl.addView(mView);
        }

    }


    public void setTitle(String title) {
        if (null != mTitleTv) {
            mTitleTv.setText(title);
        }
    }

    public void setTitle(int resId) {
        if (null != mTitleTv) {
            mTitleTv.setText(getResources().getString(resId));
        }
    }

    public void setRightText(int resId) {
        if (null != mRightTv) {
            mRightTv.setText(getResources().getString(resId));
            mRightTv.setVisibility(View.VISIBLE);
            mRightImg.setVisibility(View.GONE);
        }

    }


    public void setRightTextColor(int resId) {
        if (null != mRightTv) {
            mRightTv.setTextColor(getResources().getColor(resId));
        }

    }

    public void setLeft() {
        mWebViewBack.setVisibility(View.VISIBLE);
        mBackImg.setVisibility(View.GONE);

        mTitleWebBackLl.setVisibility(View.VISIBLE);
        mBackLl.setVisibility(View.GONE);

    }

    public void setRightImage(int resId) {
        if (null != mRightImg) {
            mRightImg.setImageDrawable(getResources().getDrawable(resId));
            mRightImg.setVisibility(View.VISIBLE);
        }

    }


}
